package com.clqteam.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.clqteam.dao.insertZsDao;
import com.clqteam.model.Product;
import com.clqteam.util.JDBCutil;

public class insertZsDaoImpl implements insertZsDao{

	@Override
	public int insertZsByAllInfo(int zspId, String zspName, int zspAmount, double zspCost, String zspTime,String zspPeople,
			String zsReason) {
		int m = 0;
		StringBuffer sql = new StringBuffer();
		sql.append(" INSERT INTO s_zs (zspId,zspName,zspAmount,zspCost,zspTime,zspPeople,zsReason)");
		sql.append(" VALUES (?,?,?,?,?,?,?)");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, zspId);
			pst.setString(2, zspName);
			pst.setInt(3, zspAmount);
			pst.setDouble(4, zspCost);
			pst.setString(5, zspTime);
			pst.setString(6, zspPeople);
			pst.setString(7, zsReason);
			int rst = pst.executeUpdate();
			if (rst==1) {
				m=rst;
			}else {
				m=0;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return m;
	}

	@Override
	public int updateShByZs(int zspId, int zspAmount) {
		StringBuffer sql = new StringBuffer();
		int n = 0 ;
				sql.append(" UPDATE s_product SET pAmount = ?");
				sql.append(" WHERE pId =?");
				Connection con = JDBCutil.getConnection();
				try {
					PreparedStatement pst = con.prepareStatement(sql.toString());
					pst.setInt(1, zspAmount);
					pst.setInt(2, zspId);
					int m = pst.executeUpdate();
					if (m==1) {
						n=m;
					}else{
						n=0;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		return n;
	}

	@Override
	public void setdirInfo(String drInfo, String drTime, int eId, String eName) {
		StringBuffer sql = new StringBuffer();
		sql.append(" INSERT INTO s_dailyreport");
		sql.append(" (drTime,drInfo,eId,eName) VALUES (?,?,?,?)");
		Connection con  = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, drTime);
			pst.setString(2, drInfo);
			pst.setInt(3, eId);
			pst.setString(4, eName);
			pst.executeUpdate();
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	@Override
	public Product getProductInfoByPid(int pid) {
		Product m = new Product();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT * FROM s_product");
		sql.append(" WHERE s_product.pId=?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, pid);
			ResultSet rst = pst.executeQuery();
			while (rst.next()) {
				m.setpId(rst.getInt("pId"));
				m.setpName(rst.getString("pName"));
				m.setpAmount(rst.getInt("pAmount"));
				m.setpCost(rst.getDouble("pCost"));
				m.setSupId(rst.getInt("supId"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return m;
	}

	@Override
	public int getOidByPid(int zspId) {
		int m = 0;
		StringBuffer sql = new StringBuffer();
		 sql.append(" SELECT s_orderinfo.oId FROM s_orderinfo");
		 sql.append(" WHERE s_orderinfo.pId=?");
		 Connection con = JDBCutil.getConnection();
		 try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, zspId);
			ResultSet rst = pst.executeQuery();
			while (rst.next()) {
				m=rst.getInt("oId");
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return m;
	}

	@Override
	public void updataOrder(int oId, double oMoney) {
		 StringBuffer sql = new StringBuffer();
		 sql.append(" UPDATE s_order SET s_order.oMoney=?");
		 sql.append(" WHERE oId = ?");
		 Connection con = JDBCutil.getConnection();
		 try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setDouble(1, oMoney);
			pst.setInt(2, oId);
			
			pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void updataOrderInfo(int oId, int pAmount) {
		StringBuffer sql = new StringBuffer();
		 sql.append(" UPDATE s_orderinfo SET s_orderinfo.poAmount=?");
		 sql.append(" WHERE oId = ?");
		 Connection con = JDBCutil.getConnection();
		 try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, pAmount);
			pst.setInt(2, oId);
			
			pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
